<?php
class employee extends head {
 function employee() {
  $this->table = "employee";
  $this->getmetadata();
  $this->maintainhist = true;
  $this->header[2] = "";
  $this->header[18] = "0000-00-00";
  $_SESSION[$this->table] = $this;
 }
 function writedb() {
  if ($this->header[16] == "") $this->header[16] = date("Y-m-d H:i:s");
  parent::writedb();
 }
 function showform() {
  echo '<head>';
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS;
  echo '</head>';
	echo "<body onload='document.form.f0.focus()'>";
  echo "<h1>Employee Setup</h1>"; 
  echo "<form name='form' method=GET>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  echo $this->htmlrow('0','Code');
  echo $this->htmlrow('1','Name');

  echo "<tr><td>Leaving Date</td><td>";
//  xd($this->header);
  if (isotojd($this->header[18]) == 0) {
   $dt = " ";
  } else {
   $dt = isotoout($this->header[18]);  
  }

  echo "<input $this->disabled id=dot name=f18 size=10 maxlength=10 type=text value='$dt'>\n"; 
  if ($this->disabled != "disabled") {
   $dc = "showChooser(this, 'dot', 'chooserSpan', 1950, 2020, 'd-m-Y', false);";
   echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
   echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
   echo "</div>\n";
  }
  echo "</td>";
  echo "<td>".$this->valids[18]."</td>";  
  echo "</tr>";
  echo $this->htmlrow('19','Passcode');
  echo $this->htmlrow('20','Department');

  echo "</table>";
  echo '<input class=bt accesskey=A type=submit name=sub value=Apply>';
  echo '<input class=bt accesskey=S type=submit name=sub value=Save>';
  echo '<button class=bt onclick=rest()><span>Restart</span></button>'; 
  echo "</form>"; 
 }
 function applyfromrequest() {
  $this->valid = true;
  // Goes through request. Applies if legal, otherwise error.
    $this->valids = array_fill(0,$this->atts,'');

  if (isset($_REQUEST["f0"]) && $this->header[2] == "0") {
   // Numeric whole only
   $f = $_REQUEST["f0"];
   if(preg_match('/^[0-9-A-Z]{3,7}$/',$f)) {
    $exists = freaditem($this->table,$f) ;
    if ($exists == "") {
     $this->header[0] = $f;
    } else {
     $this->valids[0] = "That ID is already in use for ".$exists[1];
    }
   } else {
    $this->valids[0] = "Please enter a character alphanumeric 3-7 characters long";
   }
  }

  if ($f=@$_REQUEST["f1"]) {
   // name. Allowable characters A-Za-z-space apostrophe
   if(preg_match('/^[0-9a-z A-Z\'\.-]{2,45}$/',$f)) {
    $this->header[1] = $f; 
   } else {
    $this->valids[1] = "Please enter a valid name";
   }
  }
  if ($f=@$_REQUEST["f19"]) {
   // name. Allowable characters 1-9 three different digits only.
   if (strlen($f) == 3) {
    if (is_numeric($f)) {
     $a = $f[0];
     $b = $f[1];
     $c = $f[2];
     if ($a==$b || $a==$c || $a==$b) {
      $this->valids[19] = "Must be three different digits 1-9";
     } else {
      $this->header[19] = $f;
     }
    } else {
      $this->valids[19] = "Must be three different digits 1-9";
    }
   } else {
      $this->valids[19] = "Must be three different digits 1-9";
   }
  }

  if (isset($_REQUEST["f20"])) {
   // Department
   $f = $_REQUEST["f20"];
   if(preg_match('/^[0-9a-z A-Z\.-]{0,45}$/',$f)) {
    $this->header[20] = $f; 
   } else {
    $this->valids[20] = "Please enter a valid department";
   }
  }  


  if (isset($_REQUEST["f18"])) {
   $f = $_REQUEST["f18"];
   if (trim($f) != "") {
   // Date. 
   $a = dtoiso($f);
   if ($a) {
    $diff = isotojd()-isotojd($a) ;
    if ($diff > 300 || $diff < -900) {
     $this->valids[18] = "Date looks wrong - invalidated";
    } else {
     $this->header[18] = $a;
    }
   } else {
    $this->valids[18] = "Invalid date";
   }
   } else {
    $this->header[18] = "0000-00-00";
   }
  }
	
	 
  
  $this->valid = every($this->valids,"");
  if ($this->valid) {
   if ($this->header[0] == "") $this->valids[0] = "Incomplete";
   if ($this->header[1] == "") $this->valids[1] = "Incomplete";
   $this->valid = every($this->valids,"");   
  }
   
 }
}
?>